package com.jtjrenren.android.taxi.passenger.core.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiHistoryItem;
import com.jtjrenren.android.taxi.passenger.base.BaseRcyAdapter;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.jtjrenren.android.taxi.passenger.utils.UIHelper;
import com.library.utils.StringUtils;
import com.library.utils.AppUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/1/25.
 */
public class OrderHistoryAdapter extends BaseRcyAdapter<ApiHistoryItem.DataEntity> {

    public interface itemClickListener {
        void itemClick(ApiHistoryItem.DataEntity details);

        void payNow(ApiHistoryItem.DataEntity details);
    }

    private itemClickListener listener;

    public void setListener(itemClickListener listener) {
        this.listener = listener;
    }

    public OrderHistoryAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_order_history, parent, false);
        return new OrderHistoryItemHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderHistoryItemHolder historyItemHolder = (OrderHistoryItemHolder) holder;
        ApiHistoryItem.DataEntity info = list.get(position);
        historyItemHolder.tv_drivername.setText(info.getDriverName());
        historyItemHolder.tv_carcode.setText(info.getCarNum());
        historyItemHolder.tv_date.setText(info.getCreateDate());
        historyItemHolder.tv_start.setText(info.getStartAddr());
        historyItemHolder.tv_end.setText(info.getEndAddr());

        int orderCar = Integer.parseInt(info.getOrderType());
        if (orderCar == Constants.CARTYPE_SPECAR) {
            historyItemHolder.tv_cartype.setBackgroundResource(R.drawable.shape_corner_emphasizemid_emphasizemid);
            historyItemHolder.tv_cartype.setText(context.getString(R.string.yuezuche));
            historyItemHolder.tv_cartype.setTextColor(context.getResources().getColor(
                    R.color.label_white));
            historyItemHolder.bt_paynow.setText("立即支付");
        } else {
            historyItemHolder.tv_cartype.setBackgroundResource(R.drawable.shape_corner_emphasizemid_trans);
            historyItemHolder.tv_cartype.setText(context.getString(R.string.taxi));
            historyItemHolder.tv_cartype.setTextColor(context.getResources().getColor(
                    R.color.label_normal));
            historyItemHolder.bt_paynow.setText("立即支付");
        }

        int resourcType = Integer.parseInt(info.getOrderAttritute());
        if (resourcType == Constants.ORDER_SOURCE_APPOINTMENT) {
            //预约订单
            historyItemHolder.ll_record.setVisibility(View.VISIBLE);
            historyItemHolder.tv_record.setText(info.getBookingTime());
        } else {
            //即时订单
            historyItemHolder.ll_record.setVisibility(View.GONE);
        }

        int status = Integer.parseInt(StringUtils.isEmpty(info.getStatus()) ? "100" : info.getStatus());
        switch (status) {
            case Constants.ORDER_STATUS_DISPATCH_WAITING://待调度
            case Constants.ORDER_STATUS_DISPATCH_ING://正在调度
            case Constants.ORDER_STATUS_INCAR://正在接送
            case Constants.ORDER_STATUS_DISPATCH_FINISH://调度完成
            case Constants.ORDER_STATUS_PICKUP_PASSGER://去接乘客(终端:开始处理订单)
                if (resourcType == Constants.ORDER_SOURCE_APPOINTMENT) {
//                    historyItemHolder.tv_cancelrecord.setVisibility(View.VISIBLE);
                    if (!StringUtils.isEmpty(info.getBookingTime())) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                        try {
                            Date date = format.parse(info.getBookingTime());
                            Date now = new Date();
                            if (date != null && date.before(now)) {
//                                historyItemHolder.tv_cancelrecord.setVisibility(View.GONE);
                                historyItemHolder.tv_status.setText("已过期");
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }

                historyItemHolder.tv_status.setText("未完成");
                historyItemHolder.finish_con.setVisibility(View.GONE);
                historyItemHolder.bt_paynow.setVisibility(View.GONE);

                break;
            case Constants.ORDER_STATUS_FINISH://订单成功完成
            case Constants.ORDER_STATUS_ORDER_CRASHPAY://现金支付
            case Constants.ORDER_STATUS_PASSGER_INCARORPAY://司机已经提交乘客支付

//                int payStatus = Integer.parseInt(StringUtils.isEmpty(info.getPayStatus()) ? "0" : info.getPayStatus());
                int payStatus = ModuleUtils.getPayStatus(info.getPayStatus());

                if (status == Constants.ORDER_STATUS_ORDER_CRASHPAY) {
                    list.get(position).setPayStatus("1");
                    payStatus = 1;
                }

                if (payStatus == 1) {
                    historyItemHolder.bt_paynow.setVisibility(View.GONE);
                    historyItemHolder.tv_status.setText("已支付");
                } else if (payStatus == 0) {
                    historyItemHolder.bt_paynow.setVisibility(View.VISIBLE);

                    historyItemHolder.tv_status.setText("未支付");
                } else if (payStatus == -1) {
                    historyItemHolder.bt_paynow.setVisibility(View.GONE);
                    historyItemHolder.tv_status.setText("未完成");
                }

                if (!StringUtils.isEmpty(info.getActualDistance()) && Double.parseDouble(info.getActualDistance()) > 0)
                    historyItemHolder.finish_con.setVisibility(View.VISIBLE);
                else
                    historyItemHolder.finish_con.setVisibility(View.GONE);

                historyItemHolder.tv_millege.setText(info.getActualDistance());
                historyItemHolder.tv_pay.setText(info.getCustomerPay());


                break;
            case Constants.ORDER_STATUS_CANCLE_BEFOREDISPATCH://订单承接前乘客取消
            case Constants.ORDER_STATUS_CANCLE_AFTERDRIVER_ACCEPTE://订单承接后乘客取消
            case Constants.ORDER_STATUS_CANCLE_BYDRIVER://订单承接后司机取消

                historyItemHolder.tv_status.setText("已取消");
                historyItemHolder.finish_con.setVisibility(View.GONE);
                historyItemHolder.bt_paynow.setVisibility(View.GONE);

                break;
            case Constants.ORDER_STATUS_FINISH_FORCE://强制完成 (订单失效)
            case Constants.ORDER_STATUS_CLOSE_BYPLATFORM://平台关闭（手动关闭）

                historyItemHolder.tv_status.setText("已关闭");
                historyItemHolder.finish_con.setVisibility(View.GONE);
                historyItemHolder.bt_paynow.setVisibility(View.GONE);


                break;
        }

    }

    public class OrderHistoryItemHolder extends RecyclerView.ViewHolder {
        TextView tv_drivername, tv_carcode, tv_date, tv_start, tv_end, tv_record, tv_millege, tv_pay, tv_cancelrecord, tv_status, tv_cartype;
        Button bt_calldriver, bt_paynow;
        LinearLayout finish_con, ll_record;

        public OrderHistoryItemHolder(View itemView) {
            super(itemView);
            initViews();
        }

        private void initViews() {
            tv_drivername = (TextView) itemView.findViewById(R.id.order_history_item_drivername);
            tv_carcode = (TextView) itemView.findViewById(R.id.order_history_item_carcode);
            tv_date = (TextView) itemView.findViewById(R.id.order_history_item_date);
            tv_start = (TextView) itemView.findViewById(R.id.order_history_item_start);
            tv_end = (TextView) itemView.findViewById(R.id.order_history_item_end);
            tv_record = (TextView) itemView.findViewById(R.id.order_history_item_record);
            tv_millege = (TextView) itemView.findViewById(R.id.order_history_item_milege);
            tv_pay = (TextView) itemView.findViewById(R.id.order_history_item_pay);
            tv_cancelrecord = (TextView) itemView.findViewById(R.id.order_history_item_canclerecord);
            tv_status = (TextView) itemView.findViewById(R.id.order_history_item_orderstutus);
            tv_cartype = (TextView) itemView.findViewById(R.id.order_history_item_cartype);

            bt_calldriver = (Button) itemView.findViewById(R.id.order_history_item_calldriver);
            bt_paynow = (Button) itemView.findViewById(R.id.order_history_item_paynow);

            finish_con = (LinearLayout) itemView.findViewById(R.id.order_history_item_orderfinishcon);
            ll_record = (LinearLayout) itemView.findViewById(R.id.order_history_item_record_con);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.itemClick(list.get(getAdapterPosition()));
                }
            });

            bt_calldriver.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String phoneNo = list.get(getAdapterPosition()).getContactMobile();
                    if (StringUtils.isEmpty(phoneNo))
                        return;
                    UIHelper.getCustomeDialog(context, null, "是否呼叫司机?", "呼叫", "取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            AppUtils.callPhone((Activity) context, phoneNo);
                        }
                    }, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
                }
            });

            bt_paynow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ApiHistoryItem.DataEntity dataEntity = list.get(getAdapterPosition());
//                    int orderCar = Integer.parseInt(dataEntity.getOrderType());
//                    if (orderCar == Constants.CARTYPE_TAXI) {
//                        ApiWorks.upOrderStatus(dataEntity.getID(), 11, null);
//                        bt_paynow.setVisibility(View.GONE);
//                        tv_status.setText("已支付");
//                        return;
//                    }
                    if (listener != null)
                        listener.payNow(dataEntity);
                }
            });
        }
    }
}
